/**
 * Created by ZQH on 2018/1/15.
 cordova-plugin-camera 相机插件：
 ionic cordova plugin add cordova-plugin-camera
 npm install --save @ionic-native/camera

 cordova-plugin-image-picker 图片获取插件：
 ionic cordova plugin add cordova-plugin-telerik-imagepicker --variable PHOTO_LIBRARY_USAGE_DESCRIPTION="your usage message"
 npm install --save @ionic-native/image-picker

 * ImagePickerOptions的参数：
 * maximumImagesCount	最多选几张，默认 15	number
 * width	  图片宽度	number
 * height	图片高度	number
 * quality	图像质量，越高质量越好 (0-100)	number
 * outputType	输入类型，默认为 0 (FILE_URI)	number
 *
 *CameraOptions	作用	参数
 *quality	图像质量，越高质量越好 (0-100)	number
 *sourceType	图片源	PHOTOLIBRARY、CAMERA、SAVEDPHOTOALBUM
 *destinationType	图片路径	DATA_URL--返回base64字符串、FILE_URL-- 返回uri
 *encodingType	图片类型	JPEG、PNG
 *mediaType	媒体类型，这个插件也能选视频等	PICTURE、VIDEO、ALLMEDIA
 *sourceType，这个参数设置为 PHOTOLIBRARY 就会从相册取图，设置为 CAMERA 会拍照，设置为 SAVEDPHOTOALBUM 会保存图片。
 */
import { Camera, CameraOptions } from '@ionic-native/camera';
import { ImagePicker, ImagePickerOptions } from '@ionic-native/image-picker';

export class ZQHCamera{
  public static camera: Camera;
  public static imagePicker: ImagePicker;
  /* 从相册中获取多张图片 */
  public static getPictures(maxCount, callback){
    // 设置选项
    const options: ImagePickerOptions = {
      maximumImagesCount: maxCount,

      quality: 100
    };

    // 获取图片
    this.imagePicker.getPictures(options).then((results) => {
      for (let i = 0; i < results.length; i++) {
        console.log('Image URI: ' + results[i]);
      }
      callback(results);
    }, (err) => {
      console.log('获取图片失败');
    });
  }

  /* 从相册中获取一张图片 */
  public static getPicture(pictures){
    // 设置选项
    const options: CameraOptions = {
      quality: 100,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    };

    // 获取图片
    this.camera.getPicture(options).then((fileUrl) => {
       // 获取成功
      console.log(fileUrl);
      let urls =[];
      urls.push(fileUrl);
      pictures(urls);
    }, (err) => {
      console.log('获取图片失败');
    });
  }

  /* 通过拍照获取图片 */
  public static cameraGetPicture(pictures){
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    };

    this.camera.getPicture(options).then((fileUrl) => {
      // imageData is either a base64 encoded string or a file URI
      console.log(fileUrl);
      let urls = [];
      urls.push(fileUrl);
      pictures(urls);
    }, (err) => {
      // Handle error
      console.log('获取图片失败');
    });
  }
}
